--    Program: 		CAPPOIMP_SUPPLIER_CONTACT.sql
--    Description:  Import PO Supplier Contact information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Kenny Leung
--    Date:         23-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_vendor_contact_id		number;
                    		                 		
   cursor cur_vendor_contacts is
      select * from CRST_PO_VENDOR_CONTACTS
      for update;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   
   for cur in cur_vendor_contacts loop   	
     l_err_msg_tmp := '';
     
     select PO_VENDOR_CONTACTS_S.NEXTVAL 
     into l_vendor_contact_id
     FROM DUAL;
     
     update CRST_PO_VENDOR_CONTACTS
     set    vendor_contact_id_n = l_vendor_contact_id
     where  vendor_contact_id = cur.VENDOR_CONTACT_ID;
     
     begin    
       -- import data
       insert into PO_VENDOR_CONTACTS
       (
            VENDOR_CONTACT_ID       ,
            LAST_UPDATE_DATE        ,
            LAST_UPDATED_BY         ,
            VENDOR_SITE_ID          ,
            LAST_UPDATE_LOGIN       ,
            CREATION_DATE           ,
            CREATED_BY              ,
            INACTIVE_DATE           ,
            FIRST_NAME              ,
            MIDDLE_NAME             ,
            LAST_NAME               ,
            PREFIX                  ,
            TITLE                   ,
            MAIL_STOP               ,
            AREA_CODE               ,
            PHONE                   ,
            ATTRIBUTE_CATEGORY      ,
            ATTRIBUTE1              ,
            ATTRIBUTE2              ,
            ATTRIBUTE3              ,
            ATTRIBUTE4              ,
            ATTRIBUTE5              ,
            ATTRIBUTE6              ,
            ATTRIBUTE7              ,
            ATTRIBUTE8              ,
            ATTRIBUTE9              ,
            ATTRIBUTE10             ,
            ATTRIBUTE11             ,
            ATTRIBUTE12             ,
            ATTRIBUTE13             ,
            ATTRIBUTE14             ,
            ATTRIBUTE15             ,
            REQUEST_ID              ,
            PROGRAM_APPLICATION_ID  ,
            PROGRAM_ID              ,
            PROGRAM_UPDATE_DATE     ,
            CONTACT_NAME_ALT        ,
            FIRST_NAME_ALT          ,
            LAST_NAME_ALT           ,
            DEPARTMENT              
       )
       values
       (
            l_vendor_contact_id         ,
            cur.LAST_UPDATE_DATE        ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.LAST_UPDATED_BY),
            CAPPOIMP_MAPPING.GetVendorSiteIDMapping(cur.VENDOR_SITE_ID),
            cur.LAST_UPDATE_LOGIN       ,
            cur.CREATION_DATE           ,
            CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
            cur.INACTIVE_DATE           ,
            cur.FIRST_NAME              ,
            cur.MIDDLE_NAME             ,
            cur.LAST_NAME               ,
            cur.PREFIX                  ,
            cur.TITLE                   ,
            cur.MAIL_STOP               ,
            cur.AREA_CODE               ,
            cur.PHONE                   ,
            cur.ATTRIBUTE_CATEGORY      ,
            cur.ATTRIBUTE1              ,
            cur.ATTRIBUTE2              ,
            cur.ATTRIBUTE3              ,
            cur.ATTRIBUTE4              ,
            cur.ATTRIBUTE5              ,
            cur.ATTRIBUTE6              ,
            cur.ATTRIBUTE7              ,
            cur.ATTRIBUTE8              ,
            cur.ATTRIBUTE9              ,
            cur.ATTRIBUTE10             ,
            cur.ATTRIBUTE11             ,
            cur.ATTRIBUTE12             ,
            cur.ATTRIBUTE13             ,
            cur.ATTRIBUTE14             ,
            cur.ATTRIBUTE15             ,
            cur.REQUEST_ID              ,
            cur.PROGRAM_APPLICATION_ID  ,
            cur.PROGRAM_ID              ,
            cur.PROGRAM_UPDATE_DATE     ,
            cur.CONTACT_NAME_ALT        ,
            cur.FIRST_NAME_ALT          ,
            cur.LAST_NAME_ALT           ,
            cur.DEPARTMENT              
       );
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;	  	    
     
     if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_VENDOR_CONTACTS', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
